<template>
	<view style="margin: 0px 30px;" class="wrapper">
		<input placeholder="请输入老师" @input="input" :value="value" class="input" />
		<view class="seach-icon"></view>
		<view class="choose">
			<view class="item" :key="index" v-for="(item,index) in list">
				{{item.teacherName}}<view @click="split(index)" class="close"></view>
			</view>
		</view>
		<view class="mod">
			<btn class="mmp" @click="click(index)" :type="type[index]? 'active':'from'" :key="index" v-for="(item,index) in listval">{{item.teacherName}}</btn>
		</view>
	</view>
</template>

<script>
	import btn from './button.vue'
	import {seacher} from '../request/user.js'
	export default {
		props:["teachersNumbers"],
		data() {
			return {
				value: '',
				listval:[],
				list:[],
				type:[]
			};
		},
		mounted() {
			const value = uni.getStorageSync('value');
			if(value.teachersNumbers){
				const arr = value.teachersNumbers.split('/')
				arr.forEach( ele => {
					const a = ele.split('@');
					this.list.push({
						teacherName: a[0],
						teacherNumber: a[1]
					})
				})
			}
		},
		watch:{
			list(target){
				this.$emit('change',target)
				return target
			}
		},
		methods:{
			async input({detail}){
				this.value = detail.value;
				const arr = await seacher(this.value)
				console.log(arr)
				if(Array.isArray(arr)){
					this.listval = arr.map(res => {
						this.type.push(false)
						return res
					});
				}
			},
			split(index){
				const target = this.list[index]
				const i = this.listval.findIndex(res => res.teacherName == target.teacherName);
				this.type[i] = false;
				this.list.splice(index,1);
				this.$forceUpdate()
			},
			click(index){
				const target = this.listval[index];
				if(!this.list.includes(target)){
					this.type[index] = true;
					this.list.push( this.listval[index] );
				}else{
					this.type[index] = false;
					this.list.splice(index,1);
				}
			}
		},
		components:{
			btn
		}
	}
</script>

<style lang="scss" scoped>
.wrapper{
	position: relative;
	background:rgba(248,248,251,1);
	border-radius:5px;
	height: 36px;
	.mod{
		background: #F8F8FB;
		width: 100%;
		height: 250px;
		margin-top: 10px;
		border-radius: 10px;
		padding: 30px;
		box-sizing: border-box;
		.mmp{
			margin: 5px;
		}
	}
	.choose{
		display: flex;
		margin-top: 10px;
		overflow-x: auto;
		align-items: center;
		height: 40px;
		.item{
			height: 30px;
			font-size:12px;
			white-space:nowrap; 
			position: relative;
			background: #F8F8FB;
			padding: 5px 20px;
			margin-right: 10px;
			border-radius: 13px;
			text-align: center;
			line-height: 30px;
		}
		.close{
			width: 10px;
			height: 10px;
			background: url(../static/img/icon_close@3x.png) no-repeat;
			background-position: center center;
			background-size: 100% 100%;
			position: absolute;
			top: -0px;
			right: -0px;
		}
	}
	.input{
		height: 100%;
		padding-left: 10px;
	}
	.seach-icon{
		width: 14px;
		height: 14px;
		position: absolute;
		right: 20px;
		top: 50%;
		transform: translateY(-50%);
		background-image: url(../static/img/icon_search@3x.png);
		background-position: center center;
		background-repeat: no-repeat;
		background-size: 14px 14px;
	}
}
</style>
